<template>
	<view>
    <!-- 利用JSON.stringify(address) === '{}' 的方法，查看空对象的情况-->
		<view class="btn-box" v-if="JSON.stringify(address) === '{}'">
		  <button type="primary" size="mini" class="btnChooseAddress" @click="btnChooseAddress" >请选择收货地址+</button>
      <button type="default" @click="show" >调用授权</button>
		</view>
    <view class="address-box" v-else @click="btnChooseAddress">
      <view class="address-user-info">
        <text>收货人:{{address.userName}}</text>
        <view >
          <text>电话:{{address.telNumber}}</text>
          <uni-icons type="arrowright" size="16" class="uni-icons"></uni-icons>
        </view>
      </view>
      <view class="address-info">
        <view class="left">
          收货地址:
        </view>
        <view class="right">
          {{address.provinceName ? address.provinceName : ''}}{{ address.cityName + address.countyName + address.detailInfo }}
        </view>
      </view>
    </view>
    <image src="../../static/cart_border@2x.png"class="bottomImag"></image>
	</view>
</template>

<script>
  import { mapState, mapMutations } from 'vuex'
	export default {
		data() {
			return {
        // address: {}
			};
		},
    methods: {
      ...mapMutations('userAddress', ['addAddress']),
      async btnChooseAddress () { // 选择地址
      // 用户授权了之后，点击取消，再次点击就不会弹出授权页面了
      // 在用户取消授权后，会触发rejectd方法，被catch捕获到
      // err状态一旦改变，必须要取消授权了才会重新给 err, succ 赋值
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        if (err === null && succ.errMsg  === 'chooseAddress:ok') {
          this.addAddress(succ)
        }
        // 检测用户抛出的错误
        // 一旦用户第一次点击了错误，每一次都会被这个if捕获到
        if (err && err.errMsg === 'chooseAddress:fail auth deny') {
          this.reAuth() // 调用 this.reAuth() 方法，向用户重新发起授权申请
        }
      },
      async reAuth () {
        const [err2, confirmResult] = await uni.showModal({
          content: '检测到您没打开地址权限，是否去设置打开？',
          confirmText: "确认",
          cancelText: "取消",
        })  
        if (err2) return // 弹出框异常
        console.log(confirmResult)
         if (confirmResult.cancel) return uni.$showMsg('您取消了地址授权！')
         if(confirmResult.confirm) return uni.openSetting({ // 进入授权页面
           success: (settingResult) => {
             // 地址授权的值等于 true，提示用户 “授权成功”
             if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
             // 地址授权的值等于 false，提示用户 “您取消了地址授权”
             if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
           },
           fail: (data) => {
             console.log(11,data)
           }
         })
         
      },
      show () {
        uni.openSetting({ // 进入授权页面
          success: (settingResult) => {
            // 地址授权的值等于 true，提示用户 “授权成功”
            if (settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
            // 地址授权的值等于 false，提示用户 “您取消了地址授权”
            if (!settingResult.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
          },
          fail: (data) => {
            console.log(11,data)
          }
        })
      }
    },
    computed: {
      ...mapState('userAddress', ['address']),
    }
	}
</script>

<style lang="scss">
.bottomImag {
  display: block;
  width: 100%;
  height: 5px;
}
.btn-box {
  height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.address-box {
  height: 90px;
  padding: 0 10px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  .address-user-info {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    .uni-icons {
      margin-left: 5px;
    }
  }
  .address-info {
    display: flex;
    padding-bottom: 20px ;
    .left {
      flex: 2;
    }
    .right {
      flex: 8;
    }
  }
}
</style>
